<template>
  <!-- 放置一个图标 -->
  <div class="screenWrapper">
    <svg-icon
      :icon-class="isScreenFull ?'exit-fullscreen': 'fullscreen'"
      class="fullscreen"
      @click="toggleScreen"
    />
  </div>
</template>

<script>
export default {
  name: 'ScreenFull',
  data() {
    return {
      isScreenFull: false
    }
  },
  mounted() {
    document.addEventListener('fullscreenchange', () => {
      console.log('全屏进行切换了')
      // 判断一下当前是否是全屏状态 如果是非全屏状态 isScreenFull重置为false
      const isScreenFull = document.fullscreenElement
      console.log(isScreenFull)
      if (isScreenFull == null) {
        this.isScreenFull = false
      }
    })
  },
  methods: {
    toggleScreen() {
      // 如果未开启就开启 如果已开启就关闭
      if (this.isScreenFull) {
        document.exitFullscreen()
      } else {
        document.documentElement.requestFullscreen()
      }
      this.isScreenFull = !this.isScreenFull
    }
  }
}
</script>

<style lang="scss" scoped>
.screenWrapper {
  display: flex;
  align-items: center;
  margin-right: 20px;
}
.fullscreen {
  width: 20px;
  height: 20px;
  color: #fff;
  cursor: pointer;
}
</style>
